<?php
/**
 * Created by PhpStorm.
 * User: 何健喔喔喔
 * Date: 2016/12/13
 * Time: 20:25
 */
    //echo phpinfo();
    /*$tp=file_get_contents("http://baidu.com");
    echo $tp;*/
    function http_curl($url,$paths){
        //1、初始化，创建一个新的curl资源
        $ch=curl_init();
        //设置url和相应的选项
        curl_setopt($ch,CURLOPT_URL,$url);
        if(!empty($paths)){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$paths);
        }
        
        //curl_setopt($ch,CURLOPT_HEADER,0);
        
        //禁止直接输出
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        /*if (!curl_errno($ch)) {
            print_r(curl_getinfo($ch)).'br';
        }*/
        //抓取url并返回给浏览器
        $opt=curl_exec($ch);
        var_dump($opt);
        echo "fail";
        curl_close($ch);
        return $opt;
        
       
    }
    function get_token(){
        $appid="wxf2c0b4cfd0018d0e";
        $secret="55926fccdebee063eff40a6604f3b088";
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        //var_dump($url);
        $json=http_curl($url);
        //var_dump($json);
        $json=json_decode($json);
        $json=$json->access_token;
        //var_dump($json);
        //return $json->access_token;
        return $json;
        var_dump($json->access_token);
    }
    $token=get_token();

    //var_dump($token);

    //上传图片
   /* $type="image";
    $path=dirname(__FILE__)."/hhh.jpeg";
    $paths=array("media"=>"@".$path);

        
    $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
    $arr=http_curl($url,$paths);*/
    //var_dump($arr);
    

    //模板消息
    /*function get_token_temp(){
        $appid="wxf2c0b4cfd0018d0e";
        $secret="bef47bfb63466e01bc7123dc6e6ae0d1";
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        //var_dump($url);
        $json=http_curl($url);
        //var_dump($json);
        $json=json_decode($json);
        return $json->access_token;
    }
        $access_token=get_token_temp();
    function set_msg(){
        $access_token=get_token_temp();
        $formwork= '{
           "touser":"oxSrkvuWv4Hr6R4muN0TLKmWSwjs",
           "template_id":"oasLSlzdPXF-4U21JRE0lFYLsZWVVFcxY20SC6EAxx4",
           "url":"http://www.wangwenxiao.com",            
           "data":{
                   "title": {
                       "value":"这里是自己定义的标题",
                       "color":"#173177"
                   },
                   "content":{
                       "value":"这里是自定义内容，点击进入王文晓博客，啦啦啦",
                       "color":"#173177"
                   },
                   "time": {
                       "value":"这里填写时间",
                       "color":"#173177"
                   }
           }
       }';
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }*/
    //echo set_msg();

   
        $token=get_token();
        //var_dump($token);
        $url=" https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
        //var_dump($url);
        $paths=<<<php
 {
    "button": [
        {
            "name": "扫码", 
            "sub_button": [
                {
                    "type": "scancode_waitmsg", 
                    "name": "扫码带提示", 
                    "key": "rselfmenu_0_0", 
                    "sub_button": [ ]
                }, 
                {
                    "type": "scancode_push", 
                    "name": "扫码推事件", 
                    "key": "rselfmenu_0_1", 
                    "sub_button": [ ]
                }
            ]
        }, 
        {
            "name": "发图", 
            "sub_button": [
                {
                    "type": "pic_sysphoto", 
                    "name": "系统拍照发图", 
                    "key": "rselfmenu_1_0", 
                   "sub_button": [ ]
                 }, 
                {
                    "type": "pic_photo_or_album", 
                    "name": "拍照或者相册发图", 
                    "key": "rselfmenu_1_1", 
                    "sub_button": [ ]
                }, 
                {
                    "type": "pic_weixin", 
                    "name": "微信相册发图", 
                    "key": "rselfmenu_1_2", 
                    "sub_button": [ ]
                }
            ]
        }, 
        {
            "name": "发送位置", 
            "type": "location_select", 
            "key": "rselfmenu_2_0"
        },
        {
           "type": "media_id", 
           "name": "图片", 
           "media_id": "MEDIA_ID1"
        }, 
        {
           "type": "view_limited", 
           "name": "图文消息", 
           "media_id": "MEDIA_ID2"
        }
    ]
}
php;
       
        $result=http_curl($url,$paths);
        var_dump($result);
        echo 123;

    
    
    